package com.example.spring.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.example.spring.demo.common.enums.TaskStatusEnum;
import com.example.spring.demo.common.error.CustomException;
import com.example.spring.demo.common.error.ErrorCodeEnum;
import com.example.spring.demo.common.model.req.MessageTaskDTO;
import com.example.spring.demo.entity.Message;
import com.example.spring.demo.entity.SysUser;
import com.example.spring.demo.entity.Task;
import com.example.spring.demo.entity.User;
import com.example.spring.demo.mapper.MessageMapper;
import com.example.spring.demo.mapper.SysUserMapper;
import com.example.spring.demo.mapper.TaskMapper;
import com.example.spring.demo.mapper.UserMapper;
import com.example.spring.demo.service.TaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/12/29 14:45
 **/
@Service
@RequiredArgsConstructor
public class TaskServiceImpl implements TaskService {

    private final UserMapper userMapper;
    private final TaskMapper taskMapper;
    private final MessageMapper messageMapper;
    private final SysUserMapper sysUserMapper;
    @Override
    public List<Message> getTaskMessageList(int userId) {
        // 查询用户是否存在
        SysUser sysUser = sysUserMapper.selectById(userId);
        if(Objects.isNull(sysUser)) {
            throw new CustomException(ErrorCodeEnum.USER_IS_NULL);
        }

        // 查询用户的TaskList
        List<Task> taskList =  taskMapper.getUserTaskList(userId);
        if(CollUtil.isEmpty(taskList)) {
            return null;
        }

        // 查询MessageList
        List<String> messageIdList = taskList.stream().map(Task::getMessageId).toList();
        List<Message> messageList = messageMapper.getMessageList(messageIdList);
        if(CollUtil.isEmpty(messageList)) {
            return null;
        }

        // 校验taskList 中成功获取的 messageList 中messageId
        Set<String> messageIds = messageList.stream()
                .map(Message::getId)
                .collect(Collectors.toSet());
        List<Integer> sucTaskList = taskList.stream()
                .filter(task -> messageIds.contains(task.getMessageId()))
                .map(Task::getId)
                .toList();
        taskMapper.updateTaskStatus(sucTaskList, TaskStatusEnum.TASK_COMPLETED.getTaskStatusCode());

        return messageList;
    }

    @Override
    public Task getTaskById(int taskId) {
        Task task = taskMapper.getTaskById(taskId);
        if(Objects.isNull(task)) {
            throw new CustomException(ErrorCodeEnum.TASK_IS_NULL);
        }
        return task;
    }

    @Override
    public void saveMessageTaskToAdmin(String messageId) {
        List<SysUser> adminList = sysUserMapper.getUserAdmin();
        for (SysUser user : adminList) {
            taskMapper.saveTask(Task.builder()
                    .userId(user.getUserId())
                    .messageId(messageId)
                    .status(0) // 0为未触发
                    .build());
        }
    }
}
